

#include <physm.h>


void physm_que_init(physm_que_t *que, llist_t *list){

  memset(que, 0, sizeof(physm_que_t));
  que->list = list;
  que->size = physm_llist_len(list);
  pthread_mutex_init(&que->lock, NULL);

}

void physm_que_add(physm_que_t *que, physm_body_t *data){

  PHYSM_LOCK(que);
  physm_llist_add(que->list, data);
  que->size++;
  PHYSM_UNLOCK(que);

}

void physm_que_position(physm_que_t *que, int pos){

  PHYSM_LOCK(que);
  if (pos >= que->size){
    que->offset = que->size - 1;
  }
  if (pos < 0){
    que->offset = 0;
  }
  que->offset = pos;
  PHYSM_UNLOCK(que);

}

void physm_que_reset(physm_que_t *que){

  physm_que_position(que, 0);

}

physm_body_t *physm_que_next(physm_que_t *que){

  PHYSM_LOCK(que);
  if (que->offset >= que->size){
    PHYSM_UNLOCK(que);
    return NULL;
  }
  physm_body_t *data = physm_llist_get(que->list, que->offset++);
  PHYSM_UNLOCK(que);
  return data;

}
